package cn.xkai.exercise.b;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @description: 用队列实现栈
 * @author: kaixiang
 * @date: 2022-07-22
 **/
public class Solution84 {
    class MyStack {

        Queue<Integer> queue1 = new LinkedList<>();
        Queue<Integer> queue2 = new LinkedList<>();

        /**
         * Initialize your data structure here.
         */
        public MyStack() {

        }

        /**
         * Push element x onto stack.
         */
        public void push(int x) {
            queue2.offer(x);
            while (!queue1.isEmpty()) {
                queue2.offer(queue1.poll());
            }
            Queue<Integer> temp = queue1;
            queue1 = queue2;
            queue2 = temp;
        }

        /**
         * 单队列
         * @param x
         */
        public void pushOther(int x) {
            int a = queue1.size();
            queue1.offer(x);
            for (int i = 0; i < a; i++) {
                queue1.offer(queue1.poll());
            }
        }

        /**
         * Removes the element on top of the stack and returns that element.
         */
        public int pop() {
            return queue1.poll();
        }

        /**
         * Get the top element.
         */
        public int top() {
            return queue1.peek();
        }

        /**
         * Returns whether the stack is empty.
         */
        public boolean empty() {
            return queue1.isEmpty();
        }
    }
}
